package main

//go get github.com/yuin/gopher-lua
import (
	"MqttCloudProtocolServer/publiclib/dbcontrol"
	"MqttCloudProtocolServer/publiclib/define"
	"MqttCloudProtocolServer/publiclib/helplib"
	"fmt"
	"io/ioutil"
	"net/http"
	"os/exec"
	"strings"
	"time"

	"github.com/wonderivan/logger"
	lua "github.com/yuin/gopher-lua"
)

func testLuaInFunc(L *lua.LState) int {
	//将返回参数压入栈中
	L.Push(lua.LString("What is this?"))
	//返回参数为1个
	return 1
}

func testLua() {
	L := lua.NewState()
	defer L.Close()
	//注入lua中
	L.SetGlobal("luaInFunc", L.NewFunction(testLuaInFunc))
	//先获取lua中定义的函数
	if err := L.DoFile("hello.lua"); err != nil {
		panic(err)
	}
	fn := L.GetGlobal("getHello")
	err := L.CallByParam(lua.P{
		Fn:      fn,
		NRet:    1,
		Protect: true,
	}, lua.LString("World"))
	if err != nil {
		panic(err)
	}
	//这里获取函数返回值
	ret := L.Get(-1)
	//从堆栈中扔掉返回结果
	L.Pop(1)
	//打印结果
	res, ok := ret.(lua.LString)
	if ok {
		fmt.Println(string(res))
	} else {
		fmt.Println("unexcepted result")
	}
}

func testQueue() {
	define.CheckIsLog("test.log")
	var queue helplib.QueueFirstLock
	queue.SetMaxLen(20)
	queue.SetAutoReplace(true)
	inputNum := 0
	input := func(count, milsec int) {
		for i := 0; i < count; i++ {
			queue.Push(inputNum)
			inputNum++
			time.Sleep(time.Millisecond * time.Duration(milsec))
		}
	}
	output := func(count, milsec int) {
		for i := 0; i < count; i++ {
			if !queue.IsEmpty() {
				logger.Info(queue.Pull())
				time.Sleep(time.Millisecond * time.Duration(milsec))
			}
		}
	}
	logger.Info("step0")
	output(1, 0)
	input(10, 0)
	logger.Info("step1")
	output(1, 0)
	input(20, 0)
	logger.Info("step2")
	output(3, 0)
	input(20, 0)
	logger.Info("step3")
	output(3, 0)
}
func sayhelloName(w http.ResponseWriter, r *http.Request) {
	r.ParseForm()       //解析参数，默认是不会解析的
	fmt.Println(r.Form) //这些信息是输出到服务器端的打印信息
	fmt.Println("path", r.URL.Path)
	fmt.Println("scheme", r.URL.Scheme)
	fmt.Println(r.Form["url_long"])
	for k, v := range r.Form {
		fmt.Println("key:", k)
		fmt.Println("val:", strings.Join(v, ""))
	}
	fmt.Fprintf(w, "Hello astaxie!") //这个写入到w的是输出到客户端的
}

func sayhelloName2(w http.ResponseWriter, r *http.Request) {
	body, _ := ioutil.ReadAll(r.Body)
	bodyStr := string(body)
	fmt.Println(bodyStr)
	//r.Header.Set("Content-Type", "application/json")
	//w.Header().Set("Content-Type", "application/json")
	fmt.Fprint(w, string("name is oo"))
	//w.Write([]byte("??????"))
}

//mux *ServeMux
func testWeb() {
	templateDir := "D:/WEB"
	http.Handle("/", http.FileServer(http.Dir(templateDir)))
	http.HandleFunc("/get", sayhelloName)
	http.HandleFunc("/postTest", sayhelloName2)
	http.Handle("/css/", http.FileServer(http.Dir(templateDir)))
	http.Handle("/js/", http.FileServer(http.Dir(templateDir)))

	http.ListenAndServe("0.0.0.0:8080", nil)
}

func testChan() {
	ch := make(chan int, 10)
	fmt.Println(len(ch))
	ch <- 11
	fmt.Println(len(ch))
	ch <- 22
	fmt.Println(len(ch))
	ch <- 33
	fmt.Println(len(ch))
	fmt.Println(<-ch)
	fmt.Println(len(ch))
	fmt.Println(<-ch)
	fmt.Println(len(ch))
	fmt.Println(<-ch)
	fmt.Println(len(ch))
}

func testExec() {
	cmd := exec.Command("sc", "query")
	if buf, err := cmd.Output(); err != nil { // 错误处理略
		fmt.Println(err)
	} else {
		serviceString := string(buf)
		disBuf := strings.Split(serviceString, "\r")
		for i := 0; i < len(disBuf); i++ {
			if strings.Contains(disBuf[i], "SERVICE_NAME") && strings.Contains(disBuf[i+1], "DISPLAY_NAME") {
				disBuf[i] = strings.TrimSpace(strings.Trim(strings.Trim(disBuf[i], "\r"), "\n"))
				serviceName := strings.TrimSpace(strings.Trim(disBuf[i], "SERVICE_NAME:"))
				i++
				disBuf[i] = strings.TrimSpace(strings.Trim(strings.Trim(disBuf[i], "\r"), "\n"))
				disName := strings.TrimSpace(strings.Trim(disBuf[i], "DISPLAY_NAME:"))
				fmt.Println(serviceName + ":" + disName)
				if disName == "DNS Client" {
					return
				}
			}
		}
	}
	//fmt.Println(string(buf))
}
func testSqlite() {
	control := dbcontrol.CreateModeenumControl()
	res, err := control.GetAll()
	if err != nil {
		println(err.Error())
	}
	for _, item := range res {
		println(item.Name)
	}
	control.FindByID(1)
	if control.Data != nil {
		control.Data.Name = "测试"
	}
	//control.Save()
	control.FindByID(1)
	if control.Data != nil {
		println(control.Data.Name)
	}
	//control.Delete()
	control2 := dbcontrol.CreateProtocolenumControl()
	res2, err2 := control2.GetAll()
	if err2 != nil {
		println(err2.Error())
	}
	for _, item := range res2 {
		println(item.Name)
	}
}
func checkErr(err error) {
	if err != nil {
		panic(err)
	}
}
